package com.xbongbong.formula.qlexpress.operator.math;

import com.ql.util.express.Operator;
import com.ql.util.express.OperatorOfNumber;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.BusinessRuleErrorCodeEnum;


/**
 * @author: wufeng
 * @date: 2018/7/4 10:29
 * @desrcption: 返回两数相除的余数。 结果的符号与除数相同。
 */
public class OperatorMod extends Operator {

    public OperatorMod(String name){
        this.name = name;
    }

    public OperatorMod(String aAliasName, String aName, String errorInfo){
        this.aliasName = aAliasName;
        this.name = aName;
        this.errorInfo = errorInfo;
    }
    @Override
    public Object executeInner(Object[] list) throws Exception {
        return executeInner(list[0], list[1]);
    }

    public Object executeInner(Object op1,Object op2) throws Exception {
        try {
            // 只支持字节型、长整型、短整型、整形四种余数计算
            Object result = OperatorOfNumber.modulo(op1, op2);
            Number resultNum = (Number) result;
            if (op2.toString().contains("-")) {
                return -resultNum.intValue();
            } else {
                return resultNum;
            }
        } catch (Exception e){
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261017);
        }

    }
}
